package idv.loveloser.mobilemoodle;

import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class AssignmentManager extends ListActivity {
	private SharedPreferences sharedPreferences ;
	private SharedPreferences.Editor edit=null;
	private String sessionKey;
	private long client;
	private ListView assignmentsListView;
	private View bottomView;
	private Button addMoreBtn;
	private List<MdlAssignment> assignments;
	private FrameLayout assignmentRootFrameLayout;
	private View assignmentContent;
	private LinearLayout assignmentEditRoot;
	private TextView assignmentGradeTextView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.assignemnt_manager);
		assignmentRootFrameLayout=(FrameLayout) findViewById(R.id.assignmentRootFrameLayout);
		assignmentContent = getLayoutInflater().inflate(R.layout.assignment_grade, null);
		
		
		sharedPreferences=getApplicationContext().getSharedPreferences("MySettings", MODE_WORLD_WRITEABLE );
		client =sharedPreferences.getLong("client", -1L);
		sessionKey= sharedPreferences.getString("sessionKey", "");
		String allAssignmentStr=Util.getAllAssignmnets(client, sessionKey);
		Gson gson=new Gson();
		assignments=gson.fromJson(allAssignmentStr, new TypeToken<List<MdlAssignment>>(){}.getType());
		assignmentsListView=getListView();
		AssignmentManagerAdapter adapter = new AssignmentManagerAdapter(getApplicationContext());
		adapter.setAssignments(assignments);
		bottomView=getLayoutInflater().inflate(R.layout.assignment_bottom_view, null);
		assignmentsListView.addFooterView(bottomView);
		assignmentsListView.setAdapter(adapter);
		addMoreBtn=(Button) bottomView.findViewById(R.id.assignment_add);
		addMoreBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent assignmentAddIntent = new Intent(getApplicationContext(),AssignmentEditor.class);
				startActivity(assignmentAddIntent);
			}
		});
		assignmentsListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				assignmentRootFrameLayout.removeAllViews();
				LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
				assignmentRootFrameLayout.addView(assignmentContent,params);
				final MdlAssignment assignment =assignments.get(position);
				String responseStr=Util.getAssignmentGrade(client, sessionKey, assignment.getCourse(),assignment.getId());
				Gson gson =new Gson();
				final AssignmentGrade grade =gson.fromJson(responseStr, AssignmentGrade.class);
				if(grade.isHasPermission()){
					TextView assignmentGradeTextView=(TextView) assignmentContent.findViewById(R.id.assignmentGradeTextView);
					assignmentGradeTextView.setText(grade.getFinalGrade()+"");
					if(((assignment.getTimedue()*1000)>System.currentTimeMillis()) ){
						assignmentEditRoot=(LinearLayout) assignmentContent.findViewById(R.id.assignmentEditRoot);
						assignmentEditRoot.setVisibility(View.VISIBLE);
						final EditText assignmentContentMessage=(EditText) assignmentContent.findViewById(R.id.assignmentContentMessage);
						Button assignmentUploadBtn=(Button) assignmentContent.findViewById(R.id.assignmentUploadBtn);
						assignmentUploadBtn.setOnClickListener(new OnClickListener() {
							
							@Override
							public void onClick(View v) {
								String responseStr=Util.uploadAssignment(client, sessionKey, assignment.getId(), assignmentContentMessage.getText().toString());
								if(responseStr!=null)
									assignmentEditRoot.setVisibility(View.INVISIBLE);
							}
						});
					}else{
						TextView assignmentGradeReason=(TextView) assignmentContent.findViewById(R.id.assignmentGradeReason);
						assignmentGradeReason.setText("The due day was pass.");
						LinearLayout assignmentEditRoot=(LinearLayout) assignmentContent.findViewById(R.id.assignmentEditRoot);
						assignmentEditRoot.setVisibility(View.INVISIBLE);
						
					}
				}else{
					TextView assignmentGradeReason=(TextView) assignmentContent.findViewById(R.id.assignmentGradeReason);
					assignmentGradeReason.setText("You may not have permission for this operation.");
					assignmentGradeTextView=(TextView)assignmentContent.findViewById(R.id.assignmentGradeTextView);
					assignmentGradeTextView.setText(grade.getFinalGrade()+"");
					assignmentEditRoot=(LinearLayout) assignmentContent.findViewById(R.id.assignmentEditRoot);
					assignmentEditRoot.setVisibility(View.INVISIBLE);
				}
			}
		});
	}

}
